package com.smedi.ismedi.progress.core.port.adapter.service.impl;

import com.smedi.ismedi.organization.api.dto.request.OrganizationListQueryRequest;
import com.smedi.ismedi.organization.api.dto.request.OrganizationOneQueryRequest;
import com.smedi.ismedi.organization.api.dto.response.resp.OrganizationResp;
import com.smedi.ismedi.organization.api.feign.RemoteHrOrganizationFeignApi;
import com.smedi.ismedi.progress.core.domain.dto.OrganizationQuery;
import com.smedi.ismedi.progress.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.progress.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.progress.core.port.adapter.service.OrganizationRemoteService;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class OrganizationRemoteServiceImpl implements OrganizationRemoteService {
    private final RemoteHrOrganizationFeignApi remoteHrOrganizationFeignApi;
    private final AdapterConverter adapterConverter;


    @Override
    public OrganizationResp getOrganizationOne(OrganizationQuery organizationQuery) {
        OrganizationOneQueryRequest organizationOneQueryRequest = adapterConverter.organizationQueryToOrganizationOneQueryRequest(organizationQuery);
        ResponseData<OrganizationResp> responseData = remoteHrOrganizationFeignApi.getOrganizationOne(organizationOneQueryRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        return responseData.getData();
    }

    @Override
    public List<OrganizationResp> getOrganizationList(OrganizationQuery organizationQuery) {
        OrganizationListQueryRequest organizationListQueryRequest = adapterConverter.organizationQueryToOrganizationListQueryRequest(organizationQuery);
        ResponseData<List<OrganizationResp>> responseData = remoteHrOrganizationFeignApi.getOrganizationList(organizationListQueryRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        return responseData.getData();
    }
}
